#include "PointFactory.h"
#include <algorithm>

PointFactory *PointFactory::pf = 0;

PointFactory *PointFactory::getInstance(){
	if (pf == 0){
		pf = new PointFactory();
	}
	return pf;
}

void PointFactory::reset(){
	for (std::vector<Point *>::iterator iter = points.begin(); iter != points.end(); /* no increment */){
		delete (*iter);
		iter = points.erase(iter);
	}
}

Point* PointFactory::getPoint(std::bitset<15> initID, vector3 initPos){
	Point temp(initID, initPos);
	for (std::vector<Point *>::iterator iter = points.begin(); iter != points.end(); iter++){
		if ((*(*iter)) == temp){
			return (*iter);
		}
	}
	return 0;
}

Point* PointFactory::createPoint(std::bitset<15> initID, vector3 initPos, vector3 normal){
	Point *result = getPoint(initID, initPos);
	if (result){
		return result;
	}
	else{
		result = new Point(initID, initPos, normal);
		points.push_back(result);
		return result;
	}
}

void PointFactory::updateNormal(){
	for (std::vector<Point *>::iterator iter = points.begin(); iter != points.end(); iter++){
		(*iter)->calculateNormal();
	}
}

void PointFactory::averageNormal(){
	for (std::vector<Point *>::iterator iter = points.begin(); iter != points.end(); iter++){
		(*iter)->averageNormal();
	}
}